# Maintainer: George Rawlinson <grawlinson@archlinux.org>

pkgname=drone-runner-ssh
pkgver=1.0.1
pkgrel=4
pkgdesc='Drone runner that executes a pipeline on a remote machine'
arch=('loong64' 'x86_64')
url='https://github.com/drone-runners/drone-runner-ssh'
license=('custom:Polyform-Small-Business-1.0.0' 'custom:Polyform-Free-Trial-1.0.0' 'custom:Polyform-Noncommercial-1.0.0')
depends=('glibc')
makedepends=('git' 'go')
optdepends=('drone: for a local Drone instance')
options=('!lto')
_commit='b5137ad7a32deb15063ec002986af696d9f43b83'
source=(
  "$pkgname::git+$url#commit=$_commit"
  'systemd.service'
  'sysusers.conf'
  'tmpfiles.conf'
)
sha512sums=('SKIP'
            '6eaa30699fbbaeebaa824add5c1c921e74042d200ae699c393914f3c5f90ae9cdafbd6bbe9d5d555f4914ad9c37ccc897bb0ba65c87dc5fa14b975df0f3da8ab'
            'a7d51b61f054bb97f873c7f3e5512776095ed23f9eb5ac9c7a1b007d6595ccc9de0ac4224ca38cdba565bb5ec702c09889e7c7395f03dfb277bb0a5c3225942b'
            '239c3eda7cf7ffcac5df80699ec231f2797f4f35b18b26d570303a7597755d1e4958152139ddb3bb5268fe711a1fb055aa97b8d054af13653b0d5f2e35dfb537')
b2sums=('SKIP'
        '90d868e9f6932eedc814473e160c661da7cf06a3cf1adf0709b462e388130f3f149120cf36dc6bb9f6bffbca8fce7c8624aa8a0b2511ea64d9b72dc950eea746'
        '8459bbc51dd5006159041de20fc8288bed444e9ba8b48d8a95d2f6ea5a89e38e4d1f5db816c2b35f29b3219aba396bff509157a8b234105041d61c3f4504fef6'
        'f2742d5c60a3b68ecf7dab2d602b442bafcc619d838fcc74f5f186113778719797f9fa3975643c0925def2acf282c09120c186abc90b1ee5a34a7796b38f00fd')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "$pkgname"

  # create directory for build output
  mkdir -p build

  # download dependencies
  go mod download
}

build() {
  cd "$pkgname"

  # set Go flags
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"

  go build -v \
    -trimpath \
    -buildmode=pie \
    -mod=readonly\
    -modcacherw \
    -ldflags "-linkmode external -extldflags \"${LDFLAGS}\"" \
    -o build \
    .
}

check() {
  cd "$pkgname"
  go test -v ./...
}

package() {
  # systemd integration
  install -vDm644 systemd.service "$pkgdir/usr/lib/systemd/system/$pkgname.service"
  install -vDm644 sysusers.conf "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
  install -vDm644 tmpfiles.conf "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"

  cd "$pkgname"

  # binary
  install -vDm755 -t "$pkgdir/usr/bin" build/*

  # licenses
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" licenses/* LICENSE.md

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" README.md CHANGELOG.md
}
